package com.gxc.dynamic;

/**
 * 在一个由 '0' 和 '1' 组成的二维矩阵内，找到只包含 '1' 的最大正方形，并返回其面积。
 */
public class MaximalSquare {

    public static void main(String[] args) {
        char[][] matrix= {{'1','0','1','0','0'},
                {'1','0','1','1','1'},
                {'1','1','1','1','1'},
                {'1','0','0','1','0'}};
        System.out.println(maximalSquare(matrix));
    }

    public static int maximalSquare(char[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;

        int max = Integer.MIN_VALUE;
        int[][] dp = new int[row + 1][col + 1];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (matrix[i][j] == '1') {
                    dp[i+1][j+1] = Math.min( Math.min(dp[i][j], dp[i+1][j]), dp[i][j+1]) + 1;
                    max = Math.max(dp[i+1][j+1], max);
                }
            }
        }
        return max * max;
    }
}
